{% extends "gradebook/gradebookbase.html" %}

{% load files %}

{% block content %}
<span class="floatleft">
{% if TargetCategory or TargetAssignment %}
	    <a href="{% course_url apps.gradebook.views.index %}">{{course}} Grades</a>
	    {% if TargetCategory %}
		{% if TargetAssignment %}
		> <a href="{% course_url apps.gradebook.views.index TargetCategory.slug|add:"&" %}">{{TargetCategory.title}}</a>
		{% else %}
		> {{TargetCategory.title|safe}}
		{% endif %}
	    {% endif %}
	    {% if TargetAssignment %}
		> {{TargetAssignment.title|safe}}
	    {% endif %}
{% else %}
	{{course}} Grades
{% endif %}
</span>

<div class="clear"></div>
<div class="grade_table">
    {% if Categories and Grades and Metrics %}
    <table class="grade_table" border="1" cellpadding="2">
	<tr>
	    <th>
		
	    </th>
	{% for category_name in CategoryIndex %}
	{% with category=Categories|getitem:category_name %}
	    {% if TargetCategory %}
		{% for assignment,sections in category.items %}
		    {% if assignment != "info" %}
		    {% with assignment_info=sections.info %}
			{% if TargetAssignment %}
			    {% for section in sections %}
				{% if section != 'info' %}
				<th>
				    {{section.title|safe}}
				    <br>
				    {{section.total_points}} Points
				</th>
				{% endif %}
			    {% endfor %}
			{% else %}
			    <th>
				{% if assignment_info.link %}
				<a href="{% course_url apps.gradebook.views.index filters=assignment_info.link %}">{{assignment_info.title}}<a>
				{% else %}
				{{assignment_info.title|safe}}
				{% endif %}
				<br>
				{% if TargetCategory.grading == "AVERAGE" %}
				    Weight: 1.0<br>
				{% endif %}
				{% if TargetCategory.grading == "WEIGHTED_AVERAGE" %}
				    Weight: {{assignment_info.weight}}<br>
				{% endif %}
					
				{{assignment_info.total_points}} Points
			    </th>
			    {% endif %}
		    {% endwith %}
		    {% endif %}
		{% endfor %}
	    {% else %}
		{% with category_info=category.info %}
		{% if category_name != "Total" %}
		<th>
		    {% if category_info.link %}
		    <a href="{% course_url apps.gradebook.views.index filters=category_info.link %}">{{category_info.title}}<a>
		    {% else %}
		    {{category_info.title|safe}}
		    {% endif %}
		    <br>
			Weight: {{category_info.weight}}
		</th>
		{% endif %}
		{% endwith %}
	    {% endif %}
	{% endwith %}
	{% endfor %}
	<th>Total</th>
	
	</tr>
	{% for student, grades in Grades.items %}
	<tr>
	    <td>
		{{student.last_name}}, {{student.first_name}}
	    </td>
	    
	    {% for category_name in CategoryIndex %}
	    {% with assignments=grades|getitem:category_name %}
		{% for assignment,sections in assignments.items %}
		    {% if assignment != 'info' %}
		    {% if TargetAssignment %}
			{% for section,grade in sections.items %}
			{% if section != 'info' %}
			<td>
				{% get_file_list for grade as Files %}
			    {{grade.points}}
			    {% if grade.notes %}<a title="Notes: {{grade.notes}}">*</a>{% endif %}
				{% if grade.excused %}<span class="excused" title="You have been marked as excused for this section of this assignment"><u>Excused</u></span>{% endif %}
				{% if Files %}
					<ul>
						{% for file in Files %}
							<li><a href="{{file.url}}">{{file.title}} ({{file.extension}})</a>{% if file.description %} - {{file.description}}{% endif %}</li>
						{% endfor %}
					</ul>
				{% endif %}
			</td>
			
			{% endif %}
			{% endfor %}
			<td>{{sections.info.percent|floatformat:"2"}}%</td>
		    {% endif %} 
		    {% if TargetCategory and not TargetAssignment%}<td>{{sections.info.percent|floatformat:"2"}}%</td>{% endif %}
		    {% endif %}
		{% endfor %}
		{% if not TargetAssignment %}
		    {% with category=Categories|getitem:category_name %}
		    {% if category.info.grading == "SUM" %}<td>{{assignments.info.points|floatformat:"2"}}%</td>{% endif %}
		    {% if category.info.grading == "AVERAGE" %}<td>{{assignments.info.average|floatformat:"2"}}%</td>{% endif %}
		    {% if category.info.grading == "WEIGHTED_AVERAGE" %}<td>{{assignments.info.weighted_average|floatformat:"2"}}%</td>{% endif %}
		    {% endwith %}
		{% endif %}
	    {% endwith %}
	    {% endfor %}
	    {% if not TargetCategory and not TargetAssignment %}<td>{{grades.Total|floatformat:"2"}}%</td>{% endif %}
	</tr>
	{% endfor %}
	{% if course.display_grade_metrics %}
	<tr><td></td>
	{% for category in CategoryIndex %}
	    {% with metrics=Metrics|getitem:category %}
	    {% if TargetCategory %}
		{% for assignment,sections in metrics.items %}
		{% if assignment != 'info' %}
		    {% if TargetAssignment %}
			{% for section,info in sections.items %}
			{% if section != 'info' %}
			    <td><table>
			    <tr><td>Min:</td><td>{{info.min|floatformat:"2"}}</td>
			    <tr><td>Max:</td><td>{{info.max|floatformat:"2"}}</td>
			    <tr><td>Average:</td><td>{{info.average|floatformat:"2"}}</td>
			    <tr><td>Median:</td><td>{{info.median|floatformat:"2"}}</td>
			    </table></td>
			{% endif %}
			{% endfor %}
			    <td><table>
			    <tr><td>Min:</td><td>{{sections.info.min|floatformat:"2"}}%</td>
			    <tr><td>Max:</td><td>{{sections.info.max|floatformat:"2"}}%</td>
			    <tr><td>Average:</td><td>{{sections.info.average|floatformat:"2"}}%</td>
			    <tr><td>Median:</td><td>{{sections.info.median|floatformat:"2"}}%</td>
			    </table></td>
		    {% else %}
			<td><table>
			<tr><td>Min:</td><td>{{sections.info.min|floatformat:"2"}}%</td>
			<tr><td>Max:</td><td>{{sections.info.max|floatformat:"2"}}%</td>
			<tr><td>Average:</td><td>{{sections.info.average|floatformat:"2"}}%</td>
			<tr><td>Median:</td><td>{{sections.info.median|floatformat:"2"}}%</td>
			</table></td>
		    {% endif %}
		{% endif %}
		{% endfor %}
	    {% endif  %}
	    {% if not TargetAssignment %}
		<td><table>
		{% with category=Categories|getitem:category %}
		{% if category.info.grading == "SUM" %}
		    <tr><td>Min:</td><td>{{metrics.info.points.min|floatformat:"2"}}%</td>
		    <tr><td>Max:</td><td>{{metrics.info.points.max|floatformat:"2"}}%</td>
		    <tr><td>Average:</td><td>{{metrics.info.points.average|floatformat:"2"}}%</td>
		    <tr><td>Median:</td><td>{{metrics.info.points.median|floatformat:"2"}}%</td>
		{% endif %}
		{% if category.info.grading == "AVERAGE" %}
		    <tr><td>Min:</td><td>{{metrics.info.average.min|floatformat:"2"}}%</td>
		    <tr><td>Max:</td><td>{{metrics.info.average.max|floatformat:"2"}}%</td>
		    <tr><td>Average:</td><td>{{metrics.info.average.average|floatformat:"2"}}%</td>
		    <tr><td>Median:</td><td>{{metrics.info.average.median|floatformat:"2"}}%</td>
		{% endif %}
		{% if category.info.grading == "WEIGHTED_AVERAGE" %}
		    <tr><td>Min:</td><td>{{metrics.info.weighted_average.min|floatformat:"2"}}%</td>
		    <tr><td>Max:</td><td>{{metrics.info.weighted_average.max|floatformat:"2"}}%</td>
		    <tr><td>Average:</td><td>{{metrics.info.weighted_average.average|floatformat:"2"}}%</td>
		    <tr><td>Median:</td><td>{{metrics.info.weighted_average.median|floatformat:"2"}}%</td>
		{% endif %}
		{% endwith %}
		</table></td>
	    {% endif %}
	    {% endwith %}
	{% endfor %}
	    {% if not TargetCategory and not TargetAssignment %}
	    <td><table>
		<tr><td>Min:</td><td>{{Metrics.info.min|floatformat:"2"}}%</td>
		<tr><td>Max:</td><td>{{Metrics.info.max|floatformat:"2"}}%</td>
		<tr><td>Average:</td><td>{{Metrics.info.average|floatformat:"2"}}%</td>
		<tr><td>Median:</td><td>{{Metrics.info.median|floatformat:"2"}}%</td>
	    </table></td>
	    {% endif %}
	</tr>
	{% endif %}
    </table>
    {% else %}
	<p>There are no grades for this course</p>
    {% endif %}
    </form>
</div>
{% endblock content %}